import 'dart:async';
import 'dart:ui' as ui;

import 'package:chathouse/pages/questions/pre_question_page.dart';
import 'package:chathouse/public/Config.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:chathouse/util/log.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';

import '../../../model/privacy_bean.dart';
import '../../../model/service_detail.dart';
import '../../../util/color_util.dart';
import '../../route_manager/root_route_manager.dart';

/// @class : service_detail
/// @date : 2023-02-26 13:34
/// @name : LFC
/// @description :服务详情/答题入口
class ServiceDetailPage extends StatefulWidget {
  num serviceId;

  ServiceDetailPage(this.serviceId, {Key? key}) : super(key: key);

  @override
  State<ServiceDetailPage> createState() => _ServiceDetailPageState();
}

class _ServiceDetailPageState extends State<ServiceDetailPage>
    with NavigationHelper {
  ServiceDetailModel? serviceDetailModel;
  double lowImageHeight = 0;
  double topImageHeight = 0;
  double textMarginHeight = 0;
  PrivacyBeanTool? label;

  @override
  void dispose() {
    Obj.dismissLoading();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    Obj.showLoading();
    Apis.getServiceDetail(widget.serviceId).then((value) {
      final data = value?.data;
      if (data == null) {
        return;
      }
      setState(() {
        serviceDetailModel = ServiceDetailModel.fromJson(data);

        _getImageShowH(context).then((value) {
          setState(() {
            lowImageHeight = value as double;
            var ratio = 420 / 412;
            topImageHeight = ScreenUtil().screenWidth * ratio;
            textMarginHeight = ScreenUtil().screenHeight -
                topImageHeight -
                lowImageHeight -
                10.w -
                ScreenUtil().bottomBarHeight -
                ScreenUtil().statusBarHeight;

            Obj.dismissLoading();
          });
        });
      });
    }).catchError((error) {
      Obj.dismissLoading();
      LogUtil.d("getServiceDetail error $error");
    });
  }

  Future<num> _getImageShowH(context) async {
    Completer<ui.Image> completer = Completer<ui.Image>();
    NetworkImage(serviceDetailModel?.data?.lowerImg ?? "")
        .resolve(const ImageConfiguration())
        .addListener(ImageStreamListener((ImageInfo image, bool _) {
      completer.complete(image.image);
    }));

    ui.Image info = await completer.future;
    int width = info.width;
    int height = info.height;
    lowImageHeight = ScreenUtil().screenWidth * height / width;
    return lowImageHeight;
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        toolbarHeight: 0,
        backgroundColor: Colors.transparent,
        shadowColor: Colors.transparent,
        elevation: 0.0,
        systemOverlayStyle: SystemUiOverlayStyle.dark,
      ),
      backgroundColor: Colors.white,
      body: Stack(
        children: [
          ListView(
            children: [
              AspectRatio(
                aspectRatio: 412 / 420,
                child: ImageUtils.image(
                    serviceDetailModel?.data?.serviceImg ?? "",
                    imageType: ImageType.NETWORK),
              ),
              Center(
                ///箭頭
                child: SvgPicture.asset(
                  Res.icon_pull_down,
                  width: 31.w,
                  height: 32.w,
                  color: ColorUtil.hexToColor(
                      serviceDetailModel?.data?.sceColor ?? ""),
                ).setContainer(
                  margin: EdgeInsets.only(top: 20.w),
                ),
              ),
              Padding(
                padding: EdgeInsets.only(
                  top: 10.w,
                  left: 40.w,
                  right: 48.w,
                  bottom: lowImageHeight > 0 ? lowImageHeight - 20.w : 254.w,
                ),
                child: Html(
                  data: serviceDetailModel?.data?.remark ?? "",
                ),
              ).setContainer(
                  margin: EdgeInsets.only(
                      top: textMarginHeight <= 0 ? 0.w : textMarginHeight)),
            ],
          ),
          // Positioned(
          //   top: 48.w,
          //   right: 18.w,
          //   child: Row(
          //     children: [
          //       SizedBox(
          //         width: 7.w,
          //       ),
          //       ImageUtils.image(Res.icon_star, width: 11.w, height: 10.w),
          //       SizedBox(
          //         width: 2.w,
          //       ),
          //       Text(
          //         "${serviceDetailModel?.data?.serviceEvaluation ?? "0"}",
          //         style: getTextStyle(12.sp,
          //             color: MyColors.red_23299126, fontType: FontType.BOLD),
          //       ),
          //       SizedBox(
          //         width: 16.w,
          //       ),
          //       Text(
          //         "${serviceDetailModel?.data?.comUserNum ?? "0"}個評價",
          //         style: getTextStyle(
          //           12.sp,
          //           color: Colors.black,
          //           fontType: FontType.BOLD,
          //         ),
          //       )
          //     ],
          //   ).setContainer(
          //       padding: EdgeInsets.only(
          //           left: 8.w, right: 16.w, top: 4.w, bottom: 4.w),
          //       decoration: BoxDecoration(
          //           color: Colors.white,
          //           borderRadius: BorderRadius.circular(19.w),
          //           border: Border.all(
          //               width: 0.5.w, color: MyColors.gary_158158157))),
          // ),

          ///評價
          Positioned(
              bottom: 0,
              left: 0,
              right: 0,
              child: Stack(
                children: [
                  IgnorePointer(
                    child: Stack(
                      children: [
                        ImageUtils.image(
                          serviceDetailModel?.data?.lowerImg ?? "",
                          width: size.width,
                          height: lowImageHeight,
                          imageType: ImageType.NETWORK,
                        ),
                      ],
                    ),
                  ),
                  Positioned(
                    left: 20.w,
                    right: 20.w,
                    bottom: 18.w,
                    child: Row(
                      children: [
                        Expanded(
                          child: Text("上頁",
                                  style: getTextStyle(20.sp,
                                      color: Colors.white,
                                      fontType: FontType.BOLD))
                              .setContainer(
                                  alignment: Alignment.center,
                                  height: 38.w,
                                  decoration: BoxDecoration(
                                      color: MyColors.gary_155155155,
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(19.w))))
                              .setOnClickListener(() {
                            back();
                            // Get.back();
                          }),
                        ),
                        SizedBox(width: 20.w),
                        Expanded(
                          child: Text("下一頁",
                                  style: getTextStyle(20.sp,
                                      color: Colors.white,
                                      fontType: FontType.BOLD))
                              .setContainer(
                                  alignment: Alignment.center,
                                  height: 38.w,
                                  decoration: BoxDecoration(
                                      color: MyColors.brow_25118177,
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(19.w))))
                              .setOnClickListener(() {
                            Get.to(PreQuestionPage(
                              serviceDetailModel?.data?.serviceId?.toInt() ?? 0,
                              serviceDetailModel?.data?.serviceName ?? "",
                              ColorUtil.hexToColor(
                                  serviceDetailModel?.data?.titleColor ?? ""),
                              needGetExpertList:
                                  serviceDetailModel?.data?.status == "2",
                              disinfestationServiceId: serviceDetailModel
                                      ?.data?.disinfestationServiceId ??
                                  0,
                            ));
                          }),
                        ),
                      ],
                    ),
                  )
                ],
              ))

          ///lowerImg
        ],
      ),
    );
  }
}
